package com.rocky_tech.framework.xmlprase;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class Struts_config {
	public Struts_config(){
		
		
	}
	
	public static  Map<String,XmlBean> struts_xml(String struts_config_xml_path) throws Exception {
		SAXBuilder builder  = new SAXBuilder();
		Document document = builder.build(new File(struts_config_xml_path));
		Element root  = document.getRootElement();
	
		Map<String,XmlBean> rmap = new HashMap<String,XmlBean>();
	
		Element action_mapping = root.getChild("action-mapping");
		List<Element> ac = action_mapping.getChildren();
		for(Element e:ac){
			XmlBean action = new XmlBean();
			String name = e.getAttributeValue("name");
			action.setBeanName(name);
			Element formbeans = root.getChild("formbeans");
			List<Element> form = formbeans.getChildren();
			for(Element frm:form){
				if (name.equals(frm.getAttributeValue("name"))){
					String formClass = frm.getAttributeValue("class");
					action.setFormClass(formClass);
					break;
				}
				
				String class_1 = e.getAttributeValue("class");
				//System.out.println();
			}
			String path = e.getAttributeValue("path");
			action.setPath(path);
			String type = e.getAttributeValue("type");
			action.setActionClass(type);
			List<Element> forward = e.getChildren();
			Map<String,String> map = new HashMap<String,String>();
			for (Element f:forward){
				String fname = f.getAttributeValue("name");
				String fvalue = f.getAttributeValue("value");
				map.put(fname, fvalue);
				
			}
			action.setActionForward(map);
			rmap.put(path, action);
		}
		return rmap;
		
	}
}
